<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/21
 * Time: 10:02
 */
namespace Home\Controller;
use Think\Controller;
use Think\Page;
class PaylogController extends CommonController{

    public function index(){
        $p=I("get.p",1,'intval');
        $type=I('get.type',-1,'intval');
        $trade_sn=I("get.trade_sn",'','trim');
        $pay_time=I("get.pay_time",0,'intval');

        $where=['pay_status'=>1];
//        $where['user_id']=session('uid')?session('uid'):0;;
        if($type>=0){
            $where['trade_type']=$type;
        }
        if($trade_sn){
            $where['trade_sn']=" trade_sn like '{$trade_sn}%' ";
        }
        if($pay_time>0){
            $where['pay_time']=" pay_time > ".time()-3600 * 12 * $pay_time;
        }

        $Form=M('pay_log');
        $totalRow=$Form->where($where)->count();
        $pageSize=20;
        $page=new Page($totalRow,$pageSize);
        $page->rollPage=5;
        $start=($p-1)*$pageSize;
        $data=$Form->where($where)->order('id desc')->limit($start.",".$pageSize)->select();
        $page=$page->show();
        $trade_type=[0=>'普通订单支付',10=>'充值',20=>'退款',30=>'提现'];
        foreach($data as $k=>$v){
            $data[$k]['pay_time']=date("Ymd H:i:s",$v['pay_time']);
            $data[$k]['add_time']=date("Ymd H:i:s",$v['add_time']);
            $data[$k]['trade_type']=isset($trade_type[$v['trade_type']])?$trade_type[$v['trade_type']]:"未知方式";

        }

        $this->assign('trade_sn',$trade_sn);
        $this->assign('pay_time',$pay_time);
        $this->assign('type',$type);
        $this->assign('data',$data);
        $this->assign("page",$page);
        $this->display();
    }

    public function info(){
        $id=I("get.id",0,'intval');
        $where['id']=$id;
//        $where['user_id']=session('uid')?session('uid'):0;
        $data=M('pay_log')->where($where)->find();
        $trade_type=[0=>'普通订单支付',10=>'充值',20=>'退款',30=>'提现'];

        $ar=[];
        if($data){
            $ar['流水号']=$data['trade_sn'];
            $ar['支付金额']=$data['pay_money'];
            $ar['优惠券']=$data['coupon'];
            $ar['七彩币']=$data['user_money'];
            $ar['流水类型']=isset($trade_type[$data['trade_type']])?$trade_type[$data['trade_type']]:'未知类型';
            $ar['支付状态']=$data['pay_status']==1?"已支付":"未支付";
            $ar['流水添加时间']=date("Y/m/d H:i:s",$data['add_time']);
            $ar['流水更新时间']=$data['update_time']?date("Y/m/d H:i:s",$data['update_time']):" ";
            $ar['支付人']=$data['user_id'];
            $ar['支付方式']=$data['pay_name'];
            $ar['支付时间']=$data['pay_time']?date("Y/m/d H:i:s",$data['pay_time']):' ';
            $this->assign('info',$data);
        }

        if($data['pid']){
            $data=M('pay_log')->where(['id'=>$data['pid']])->find();
        }
        $order=[];
        if(in_array($data['trade_type'],[0,20])){
            $order_field=['order_sn','store_name','add_time','trade_sn','id'];
            $order=M('order')->field($order_field)->where(['trade_sn'=>$data['trade_sn']])->select();
            $order_id_ar=array_column($order,'id');
            $order_id_ar=empty($order_id_ar)?[0]:$order_id_ar;
            $order_goods=M('order_goods')->field("order_id,goods_id,goods_name,goods_spec,goods_price,goods_num")->where("order_id in (".join(',',$order_id_ar).") ")->select();
            $order_goods_ar=[];
            foreach($order_goods as $k=>$v){
                 $order_goods_ar[$v['order_id']][$v['goods_id']]=$v;
            }
            foreach($order as $k=>$v){
                $order[$k]['goods_list']=$order_goods_ar[$v['id']];
            }
        }

        $this->assign('order',$order);
        $this->assign('data',$ar);
        $this->display();
    }

}